<?php

namespace joy;

class Tree
{
    public static function arr2tree(array $data, $pid = 'pid', $child = 'child')
    {
        $tree = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v[$child] = self::arr2tree($data, $v['id']);
                $tree[] = $v;
                unset($data[$k]);
            }
        }
        return $tree;
    }
}